$(function (){
	try {
		CheckUseTimeStr();
	}catch (e){}
	try {
		LoadApiRate();
	}catch (e) {}
	try {
		CheckApiID();
	}catch (e) {}

	WindowHeight(0);setTimeout("WindowHeight(-1)",500);
});


// 检测表单
function CheckForm(){
	if ($id('typeID').value==""){ alert("请选择产品类型！");$id('typeID').focus();return false; }
	if ($id('theme').value==""){ alert("请选择产品名称！");$id('theme').focus();return false; }
	if (SelectGetText('typeID').indexOf('[api:2]') != -1){
		if ($id('jinganProType').value==""){ alert("请选择类型(景安)！");$id('jinganProType').focus();return false; }
	}else if (SelectGetText('typeID').indexOf('[api:1]') != -1 || SelectGetText('typeID').indexOf('[api:6]') != -1){
		if (ToInt($id('comProID').value) <= 0){ alert("请输入上级渠道商产品id！");$id('comProID').focus();return false; }
//		if (ToInt($id('comServID').value) <= 0){ alert("请输入上级渠道商服务器id！");$id('comServID').focus();return false; }
	}
	$id('apiID').value = GetSignStr(SelectGetText('typeID'),'[api:',']',false,false);
	var useTimeNum = 0;
	for (var i=0; i<$name('useTimeStr[]').length; i++){
		if ($name('useTimeStr[]')[i].checked){ useTimeNum++; }
	}
	if (useTimeNum == 0){ alert("请选择允许开通期限！");return false; }

	if (ToFloat($id('priceMonth').value)==0){ alert("月付价格不能为空或为0！");$id('priceMonth').focus();return false; }
	if (ToFloat($id('costMonth').value)==0){ alert("月付成本不能为空或为0！");$id('costMonth').focus();return false; }
	if (ToFloat($id('rateMonth').value)==0){ alert("月付费率不能为空或为0！");$id('rateMonth').focus();return false; }
	if ($id('uts_day').checked){
		if (ToFloat($id('priceDay').value)==0){ alert("日付价格不能为空或为0！");$id('priceDay').focus();return false; }
		if (ToFloat($id('costDay').value)==0){ alert("日付成本不能为空或为0！");$id('costDay').focus();return false; }
		if (ToFloat($id('rateDay').value)==0){ alert("日付费率不能为空或为0！");$id('rateDay').focus();return false; }
	}
	if ($id('uts_week').checked){
		if (ToFloat($id('priceWeek').value)==0){ alert("周付价格不能为空或为0！");$id('priceWeek').focus();return false; }
		if (ToFloat($id('costWeek').value)==0){ alert("周付成本不能为空或为0！");$id('costWeek').focus();return false; }
		if (ToFloat($id('rateWeek').value)==0){ alert("周付费率不能为空或为0！");$id('rateWeek').focus();return false; }
	}
	if ($id('uts_3month').checked){
		if (ToFloat($id('price3Month').value)==0){ alert("季付价格不能为空或为0！");$id('price3Month').focus();return false; }
		if (ToFloat($id('cost3Month').value)==0){ alert("季付成本不能为空或为0！");$id('cost3Month').focus();return false; }
		if (ToFloat($id('rate3Month').value)==0){ alert("季付费率不能为空或为0！");$id('rate3Month').focus();return false; }
	}
	if ($id('uts_6month').checked){
		if (ToFloat($id('price6Month').value)==0){ alert("半年付价格不能为空或为0！");$id('price6Month').focus();return false; }
		if (ToFloat($id('cost6Month').value)==0){ alert("半年付成本不能为空或为0！");$id('cost6Month').focus();return false; }
		if (ToFloat($id('rate6Month').value)==0){ alert("半年付费率不能为空或为0！");$id('rate6Month').focus();return false; }
	}
	if ($id('uts_year').checked){
		if (ToFloat($id('priceYear').value)==0){ alert("一年付价格不能为空或为0！");$id('priceYear').focus();return false; }
		if (ToFloat($id('costYear').value)==0){ alert("一年付成本不能为空或为0！");$id('costYear').focus();return false; }
		if (ToFloat($id('rateYear').value)==0){ alert("一年付费率不能为空或为0！");$id('rateYear').focus();return false; }
	}
	if ($id('uts_2year').checked){
		if (ToFloat($id('price2Year').value)==0){ alert("二年付价格不能为空或为0！");$id('price2Year').focus();return false; }
		if (ToFloat($id('cost2Year').value)==0){ alert("二年付成本不能为空或为0！");$id('cost2Year').focus();return false; }
		if (ToFloat($id('rate2Year').value)==0){ alert("二年付费率不能为空或为0！");$id('rate2Year').focus();return false; }
	}
	if ($id('uts_3year').checked){
		if (ToFloat($id('price3Year').value)==0){ alert("三年付价格不能为空或为0！");$id('price3Year').focus();return false; }
		if (ToFloat($id('cost3Year').value)==0){ alert("三年付成本不能为空或为0！");$id('cost3Year').focus();return false; }
		if (ToFloat($id('rate3Year').value)==0){ alert("三年付费率不能为空或为0！");$id('rate3Year').focus();return false; }
	}
	try{
		if ($id('uts_4year').checked){
			if (ToFloat($id('price4Year').value)==0){ alert("四年付价格不能为空或为0！");$id('price4Year').focus();return false; }
			if (ToFloat($id('cost4Year').value)==0){ alert("四年付成本不能为空或为0！");$id('cost4Year').focus();return false; }
			if (ToFloat($id('rate4Year').value)==0){ alert("四年付费率不能为空或为0！");$id('rate4Year').focus();return false; }
		}
		if ($id('uts_5year').checked){
			if (ToFloat($id('price5Year').value)==0){ alert("五年付价格不能为空或为0！");$id('price5Year').focus();return false; }
			if (ToFloat($id('cost5Year').value)==0){ alert("五年付成本不能为空或为0！");$id('cost5Year').focus();return false; }
			if (ToFloat($id('rate5Year').value)==0){ alert("五年付费率不能为空或为0！");$id('rate5Year').focus();return false; }
		}
	}catch (e){}
	return true
}


// 检测付款方式
function CheckUseTimeStr(){
	for (var i=0; i<$name('useTimeStr[]').length; i++){
		if ($name('useTimeStr[]')[i].value != '|24hour|' && $name('useTimeStr[]')[i].value != '|48hour|' && $name('useTimeStr[]')[i].value != '|month|'){
			if ($name('useTimeStr[]')[i].checked){
				$id('box_'+ $name('useTimeStr[]')[i].value.replace(/\|/g,'')).style.display = '';
			}else{
				$id('box_'+ $name('useTimeStr[]')[i].value.replace(/\|/g,'')).style.display = 'none';
			}
		}
	}
}


// 自动计算
function CalcPriceCost(){
	if (ToFloat($id('priceMonth').value)==0){ alert("月付价格不能为空或为0！");$id('priceMonth').focus();return false; }
	if (ToFloat($id('costMonth').value)==0){ alert("月付成本不能为空或为0！");$id('costMonth').focus();return false; }
	if (ToFloat($id('rateMonth').value)==0){ alert("月付费率不能为空或为0！");$id('rateMonth').focus();return false; }
	$id('priceDay').value	= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rateDay').value),$id('rateMonth').value));
	$id('costDay').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rateDay').value),$id('rateMonth').value));
	$id('priceWeek').value	= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rateWeek').value),$id('rateMonth').value));
	$id('costWeek').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rateWeek').value),$id('rateMonth').value));
	$id('price3Month').value= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rate3Month').value),$id('rateMonth').value));
	$id('cost3Month').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rate3Month').value),$id('rateMonth').value));
	$id('price6Month').value= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rate6Month').value),$id('rateMonth').value));
	$id('cost6Month').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rate6Month').value),$id('rateMonth').value));
	$id('priceYear').value	= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rateYear').value),$id('rateMonth').value));
	$id('costYear').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rateYear').value),$id('rateMonth').value));
	$id('price2Year').value	= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rate2Year').value),$id('rateMonth').value));
	$id('cost2Year').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rate2Year').value),$id('rateMonth').value));
	$id('price3Year').value	= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rate3Year').value),$id('rateMonth').value));
	$id('cost3Year').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rate3Year').value),$id('rateMonth').value));
	$id('price4Year').value	= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rate4Year').value),$id('rateMonth').value));
	$id('cost4Year').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rate4Year').value),$id('rateMonth').value));
	$id('price5Year').value	= formatFlt2(accDiv(accMul($id('priceMonth').value,$id('rate5Year').value),$id('rateMonth').value));
	$id('cost5Year').value	= formatFlt2(accDiv(accMul($id('costMonth').value,$id('rate5Year').value),$id('rateMonth').value));
}

// 计算单个费率
function CalcRate(str){
	if (ToFloat($id('priceMonth').value)==0){ alert("月付价格不能为空或为0！");$id('priceMonth').focus();return false; }
	switch (str){
		case 'day':
			if (ToFloat($id('priceDay').value)==0){ alert("日付价格不能为空或为0！");$id('priceDay').focus();return false; }
			$id('rateDay').value	= formatFlt4(accDiv($id('priceDay').value,$id('priceMonth').value));
			break;

		case 'week':
			if (ToFloat($id('priceWeek').value)==0){ alert("周付价格不能为空或为0！");$id('priceWeek').focus();return false; }
			$id('rateWeek').value	= formatFlt4(accDiv($id('priceWeek').value,$id('priceMonth').value));
			break;

		case '3month':
			if (ToFloat($id('price3Month').value)==0){ alert("季付价格不能为空或为0！");$id('price3Month').focus();return false; }
			$id('rate3Month').value	= formatFlt4(accDiv($id('price3Month').value,$id('priceMonth').value));
			break;

		case '6month':
			if (ToFloat($id('price6Month').value)==0){ alert("半年付价格不能为空或为0！");$id('price6Month').focus();return false; }
			$id('rate6Month').value	= formatFlt4(accDiv($id('price6Month').value,$id('priceMonth').value));
			break;

		case 'year':
			if (ToFloat($id('priceYear').value)==0){ alert("年付价格不能为空或为0！");$id('priceYear').focus();return false; }
			$id('rateYear').value	= formatFlt4(accDiv($id('priceYear').value,$id('priceMonth').value));
			break;

		case '2year':
			if (ToFloat($id('price2Year').value)==0){ alert("两年付价格不能为空或为0！");$id('price2Year').focus();return false; }
			$id('rate2Year').value	= formatFlt4(accDiv($id('price2Year').value,$id('priceMonth').value));
			break;

		case '3year':
			if (ToFloat($id('price3Year').value)==0){ alert("三年付价格不能为空或为0！");$id('price3Year').focus();return false; }
			$id('rate3Year').value	= formatFlt4(accDiv($id('price3Year').value,$id('priceMonth').value));
			break;

		case '4year':
			if (ToFloat($id('price4Year').value)==0){ alert("四年付价格不能为空或为0！");$id('price4Year').focus();return false; }
			$id('rate4Year').value	= formatFlt4(accDiv($id('price4Year').value,$id('priceMonth').value));
			break;

		case '5year':
			if (ToFloat($id('price5Year').value)==0){ alert("五年付价格不能为空或为0！");$id('price5Year').focus();return false; }
			$id('rate5Year').value	= formatFlt4(accDiv($id('price5Year').value,$id('priceMonth').value));
			break;

	}
}


// 设置产品名称
function SetThemeValue(){
	if ($id('theme').value == ''){ $id('theme').value=SelectGetText('typeID').replace(GetSignStr(SelectGetText('typeID'),'（公司','）',true,true),''); }
}


// 读取产品类型名称
function LoadVpsTypeText(){
	if ($id('typeID').value == ''){ alert("请先选择产品名称，再点击！");$id('typeID').focus();return false; }
	$id('theme').value = SelectGetText('typeID').replace(GetSignStr(SelectGetText('typeID'),'（公司','）',true,true),'');
}


// 读取类型(景安)名称
function LoadJinganTypeText(){
	if ($id('jinganProType').value == ''){ alert("请先选择类型(景安)，再点击！");$id('jinganProType').focus();return false; }
	$id('theme').value = SelectGetText('jinganProType');
}


// 读取API接口的费率信息
function LoadApiRate(){
	if ($id('typeID').value != ''){
		AjaxGetDeal('vpsApiData_deal.php?mudi=loadApiRate&typeID='+ $id('typeID').value);
	}
}


// 检测显示API专属选项
function CheckApiID(){
	if (SelectGetText('typeID').indexOf('[api:1]') != -1){
		$id('api1Box').style.display = '';
		$id('api6Box').style.display = '';
	}else{
		$id('api1Box').style.display = 'none';
		if (SelectGetText('typeID').indexOf('[api:6]') != -1){
			$id('api6Box').style.display = '';
		}else{
			$id('api6Box').style.display = 'none';
		}
	}
	if (SelectGetText('typeID').indexOf('[api:2]') != -1){
		$id('api2Box').style.display = '';
	}else{
		$id('api2Box').style.display = 'none';
	}
	WindowHeight(0);
}


// 设置空白栏费率
function SetRate(){
	if (ToFloat($id('rateDayApi').value) == 0){ $id('rateDayApi').value = $id('rateDayApiOld').value; }
	if (ToFloat($id('rateWeekApi').value) == 0){ $id('rateWeekApi').value = $id('rateWeekApiOld').value; }
	if (ToFloat($id('rateMonthApi').value) == 0){ $id('rateMonthApi').value = $id('rateMonthApiOld').value; }
	if (ToFloat($id('rate3MonthApi').value) == 0){ $id('rate3MonthApi').value = $id('rate3MonthApiOld').value; }
	if (ToFloat($id('rate6MonthApi').value) == 0){ $id('rate6MonthApi').value = $id('rate6MonthApiOld').value; }
	if (ToFloat($id('rateYearApi').value) == 0){ $id('rateYearApi').value = $id('rateYearApiOld').value; }
	if (ToFloat($id('rate2YearApi').value) == 0){ $id('rate2YearApi').value = $id('rate2YearApiOld').value; }
	if (ToFloat($id('rate3YearApi').value) == 0){ $id('rate3YearApi').value = $id('rate3YearApiOld').value; }
	if (ToFloat($id('rate4YearApi').value) == 0){ $id('rate4YearApi').value = $id('rate4YearApiOld').value; }
	if (ToFloat($id('rate5YearApi').value) == 0){ $id('rate5YearApi').value = $id('rate5YearApiOld').value; }

	var rateDayNew='',rateWeekNew='',rateMonthNew='',rate3MonthNew='',rate6MonthNew='',rateYearNew='',rate2YearNew='',rate3YearNew='',rate4YearNew='',rate5YearNew='';
	if (ToFloat($id('rateDayApi').value) != ToFloat($id('rateDayApiOld').value)){
		rateDayNew = '参考费率：'+ $id('rateDayApiOld').value;
	}
	if (ToFloat($id('rateWeekApi').value) != ToFloat($id('rateWeekApiOld').value)){
		rateWeekNew = '参考费率：'+ $id('rateWeekApiOld').value;
	}
	if (ToFloat($id('rateMonthApi').value) != ToFloat($id('rateMonthApiOld').value)){
		rateMonthNew = '参考费率：'+ $id('rateMonthApiOld').value;
	}
	if (ToFloat($id('rate3MonthApi').value) != ToFloat($id('rate3MonthApiOld').value)){
		rate3MonthNew = '参考费率：'+ $id('rate3MonthApiOld').value;
	}
	if (ToFloat($id('rate6MonthApi').value) != ToFloat($id('rate6MonthApiOld').value)){
		rate6MonthNew = '参考费率：'+ $id('rate6MonthApiOld').value;
	}
	if (ToFloat($id('rateYearApi').value) != ToFloat($id('rateYearApiOld').value)){
		rateYearNew = '参考费率：'+ $id('rateYearApiOld').value;
	}
	if (ToFloat($id('rate2YearApi').value) != ToFloat($id('rate2YearApiOld').value)){
		rate2YearNew = '参考费率：'+ $id('rate2YearApiOld').value;
	}
	if (ToFloat($id('rate3YearApi').value) != ToFloat($id('rate3YearApiOld').value)){
		rate3YearNew = '参考费率：'+ $id('rate3YearApiOld').value;
	}
	if (ToFloat($id('rate4YearApi').value) != ToFloat($id('rate4YearApiOld').value)){
		rate4YearNew = '参考费率：'+ $id('rate4YearApiOld').value;
	}
	if (ToFloat($id('rate5YearApi').value) != ToFloat($id('rate5YearApiOld').value)){
		rate5YearNew = '参考费率：'+ $id('rate5YearApiOld').value;
	}
	$id('rateDayApiStr').innerHTML = rateDayNew;
	$id('rateWeekApiStr').innerHTML = rateWeekNew;
	$id('rateMonthApiStr').innerHTML = rateMonthNew;
	$id('rate3MonthApiStr').innerHTML = rate3MonthNew;
	$id('rate6MonthApiStr').innerHTML = rate6MonthNew;
	$id('rateYearApiStr').innerHTML = rateYearNew;
	$id('rate2YearApiStr').innerHTML = rate2YearNew;
	$id('rate3YearApiStr').innerHTML = rate3YearNew;
	$id('rate4YearApiStr').innerHTML = rate4YearNew;
	$id('rate5YearApiStr').innerHTML = rate5YearNew;
}


// 下面价格费率使用
function LoadPriceRate(){
	$id('rateDay').value = $id('rateDayApi').value;
	$id('rateWeek').value = $id('rateWeekApi').value;
	$id('rateMonth').value = $id('rateMonthApi').value;
	$id('rate3Month').value = $id('rate3MonthApi').value;
	$id('rate6Month').value = $id('rate6MonthApi').value;
	$id('rateYear').value = $id('rateYearApi').value;
	$id('rate2Year').value = $id('rate2YearApi').value;
	$id('rate3Year').value = $id('rate3YearApi').value;
	$id('rate4Year').value = $id('rate4YearApi').value;
	$id('rate5Year').value = $id('rate5YearApi').value;
	SetRate();
}


// 恢复默认费率
function LoadDefRate(){
	if ($id('rateDayApiOld').value + $id('rateWeekApiOld').value + $id('rateMonthApiOld').value + $id('rate6MonthApiOld').value + $id('rateYearApiOld').value == ''){
		alert('无费率信息，请先选择 产品类型');$id('typeID').focus();return false;
	}
	$id('rateDayApi').value = $id('rateDayApiOld').value;
	$id('rateWeekApi').value = $id('rateWeekApiOld').value;
	$id('rateMonthApi').value = $id('rateMonthApiOld').value;
	$id('rate3MonthApi').value = $id('rate3MonthApiOld').value;
	$id('rate6MonthApi').value = $id('rate6MonthApiOld').value;
	$id('rateYearApi').value = $id('rateYearApiOld').value;
	$id('rate2YearApi').value = $id('rate2YearApiOld').value;
	$id('rate3YearApi').value = $id('rate3YearApiOld').value;
	$id('rate4YearApi').value = $id('rate4YearApiOld').value;
	$id('rate5YearApi').value = $id('rate5YearApiOld').value;
	SetRate();
}


// 批量设置
function MoreSetTo(){
	if ($id('moreSetTo').value==""){ return false; }
	var selNum = 0;
	for (var i=0;i<$name("selDataID[]").length;i++){
		if ($name("selDataID[]")[i].checked){
			selNum ++;
		}
	}
	if (selNum==0){
		alert('请先选择要设置的记录.');$id('moreSetTo').value="";return false;
	}
	selOptionText = $id('moreSetTo').options[$id('moreSetTo').selectedIndex].text;
	selOptionValue = $id('moreSetTo').value;
	$id('moreSetToCN').value = selOptionText;

	if (confirm("你确定要批量设置成【"+ selOptionText +"】？")==true){
		$id('listForm').action="vpsApiData_deal.php?mudi=moreSet";
		$id('listForm').submit();
	}else{
		$id('moreSetTo').value="";
	}
	
}
